package com.itmk.web.companyInfo.service.impl;

import com.itmk.web.adminInfo.entity.ForjInfo;
import com.itmk.web.companyInfo.entity.*;
import com.itmk.web.companyInfo.mapper.CompanyMapper;
import com.itmk.web.companyInfo.service.CompanyService;
import com.itmk.web.userInfo.entity.TokenUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;
    @Override
    public Company getCompanyInfo(String uuid) {
        Company company = companyMapper.getCompanyInfo(uuid);
        Show show = new Show();
        show.setCollect(company.getCollect());
        show.setPositions(company.getPositions());
        show.setVisits(company.getVisits());
        show.setOffer(company.getOffer());
        company.setShow(show);
        return company;
    }

    @Override
    public void updateLogo(String fileName,String uuid) {
        companyMapper.updateCompanyLogo(fileName,uuid);
    }

    @Override
    public Boolean updateCompanyInfo(Company company, String uuid) {
        return companyMapper.updateCompanyInfo(company,uuid);
    }

    @Override
    public Boolean addJobInfo(JobInfo jobInfo, String uuid) {
        return companyMapper.addJobInfo(jobInfo,uuid);
    }

    @Override
    public boolean deleteJobInfo(String job_uuid, String uuid) {
        return companyMapper.deleteJobInfo(job_uuid,uuid);
    }

    @Override
    public List<JobInfo> allCompanyJob(String uuid) {
        List<JobInfo> jobInfos = companyMapper.allCompanyJob(uuid);
        for (JobInfo result : jobInfos) {
            Salary salary = new Salary();
            salary.setMsalaryMax(result.getMsalaryMax());
            salary.setMsalaryMin(result.getMsalaryMin());
            salary.setDsalaryMax(result.getDsalaryMax());
            salary.setDsalaryMin(result.getDsalaryMin());
            salary.setNumMounth(result.getNumMounth());
            result.setSalary(salary);
        }
        return jobInfos;
    }

    @Override
    public JobInfo getOneJob(String jobUuid) {
        JobInfo result = companyMapper.getOneJob(jobUuid);
        Salary salary = new Salary();
        salary.setMsalaryMax(result.getMsalaryMax());
        salary.setMsalaryMin(result.getMsalaryMin());
        salary.setDsalaryMax(result.getDsalaryMax());
        salary.setDsalaryMin(result.getDsalaryMin());
        salary.setNumMounth(result.getNumMounth());
        result.setSalary(salary);
        return result;
    }

    @Override
    public List<JobInfo> getJobBySkill(String skill, String uuid) {

        List<JobInfo> jobInfos = companyMapper.getJobBySkill(skill,uuid);
        for (JobInfo result : jobInfos) {
            Salary salary = new Salary();
            salary.setMsalaryMax(result.getMsalaryMax());
            salary.setMsalaryMin(result.getMsalaryMin());
            salary.setDsalaryMax(result.getDsalaryMax());
            salary.setDsalaryMin(result.getDsalaryMin());
            salary.setNumMounth(result.getNumMounth());
            result.setSalary(salary);
        }
        return jobInfos;
    }

    @Override
    public Boolean updateJobInfo(JobInfo jobInfo, String uuid) {
        return companyMapper.updateJobInfo(jobInfo,uuid);
    }

    @Override
    public boolean addResume(AddResume addResume) {
        JobInfo jobinfo = companyMapper.getResume(addResume.getJob_uuid());
        String oldResume = jobinfo.getResumes();
        String newResume;
        if(oldResume==null){
            newResume = addResume.getUserInfo_uuid();
        }else{
            newResume = oldResume+","+addResume.getUserInfo_uuid();
        }
        return companyMapper.addResume(addResume.getJob_uuid(),newResume);
    }

    @Override
    public List<String> getAllResume(String jobUuid) {
        String resume = companyMapper.getResume(jobUuid).getResumes();
        String[] resumes = resume.split(",");
        List<String> resumeList = new ArrayList<>();
        for (String s : resumes) {
            resumeList.add(s);
        }
        return resumeList;
    }

    @Override
    public TokenUUID getTokenUUID(String uuid) {
        return companyMapper.getTokenUUID(uuid);
    }

    @Override
    public String checkFile(String uuid) {
        return companyMapper.checkFile(uuid);
    }

    @Override
    public String getResumePDF(String user) {
        return companyMapper.getResumePDF(user);
    }

    @Override
    public ForjInfo getForJobInfo(String uuid) {
        ForjInfo result = companyMapper.getForJobInfo(uuid);
        return result;
    }

    @Override
    public List<JobInfo> getJobFirst(Salary salary, String city, String forType) {
        //初步检索出符合城市，或者期待类型的岗位
        return companyMapper.getJobFirst(salary.getNumMounth(),
                salary.getMsalaryMax(),
                salary.getMsalaryMin(),
                salary.getDsalaryMax(),
                salary.getDsalaryMin(),
                city,forType);
    }

    @Override
    public Boolean updateNameAndIden(String linkman, String linkmanIden,String uuid) {
        return companyMapper.updateNameAndIden(linkman,linkmanIden,uuid);
    }

    @Override
    public Boolean CollectJobs(CollectJob collectJob) {
        String job_uuid = collectJob.getJobUuid();
        String user_uuid = collectJob.getUserUuid();
        return companyMapper.CollectJobs(job_uuid,user_uuid);
    }

    @Override
    public Boolean updateJobCollect(String companyUUID) {
        int jobCollect = companyMapper.getJobCollect(companyUUID)+1;
        Boolean newCollect = companyMapper.setJobCollect(jobCollect,companyUUID);
        return newCollect;
    }

    @Override
    public Boolean updatejobVisits(VisitsJob visitsJob) {
        String jobUUID = visitsJob.getJobUuid();
        String userUUID = visitsJob.getUserUuid();
        return companyMapper.updatejobVisits(jobUUID,userUUID);
    }

    @Override
    public CollectJob getCollect(CollectJob collectJob) {
        return companyMapper.getCollect(collectJob.getJobUuid(),collectJob.getUserUuid());
    }

    @Override
    public void cancelCollect(String jobUuid, String uuid) {
        companyMapper.cancelCollect(jobUuid,uuid);
    }

    @Override
    public Boolean updateBossvisits(VisitsJob visitsJob) {
        int jobvisits = companyMapper.getJobVisits(visitsJob.getCompanyUuid())+1;
        Boolean newVisits = companyMapper.setJobVisits(jobvisits,visitsJob.getCompanyUuid());
        return newVisits;
    }

    @Override
    public int getJobNum(String uuid) {
        return companyMapper.getJobNum(uuid);
    }

    @Override
    public void insertOffer(Offer offer) {
        companyMapper.insertOffer(offer.getJobUuid(),offer.getCompanyUuid(),offer.getUserUuid());
    }

    @Override
    public void updateOfferNum(String uuid) {
        int offerNum = companyMapper.getOfferNum(uuid)+1;
        companyMapper.updateOfferNum(offerNum,uuid);
    }

    @Override
    public Show getShow(String uuid) {
        Company company =  companyMapper.getCompanyInfo(uuid);
        Show show = new Show();
        show.setCollect(company.getCollect());
        show.setPositions(company.getPositions());
        show.setVisits(company.getVisits());
        show.setOffer(company.getOffer());
        return show;
    }

}
